#include "beep_driver.h"
#include "zf_driver_gpio.h"
struct beep_driver_s beep;



void set_beep(float on_time, float close_time, int num)
{
    if(beep.loop_state == off)
    {
        beep.loop_state = on;
        beep.on_time = on_time;
        beep.close_time = close_time;
        beep.num = num;
    }
}

void beep_on(void)
{
    if (beep.state == off)
    {
        gpio_set_level(P33_10, 1);/* code */
        beep.state = on;
    }
}

void beep_off(void)
{
    if (beep.state == on)
    {
        gpio_set_level(P33_10, 0);/* code */
        beep.state = off;
    }
    
}

void beep_loop(void)
{
    if (beep.loop_state == on)
    {
        
        if (beep.now_time <= beep.on_time)
        {
            beep_on();
        }
        else if (beep.now_time <= beep.on_time + beep.close_time)
        {
            beep_off();
        }
        else if (beep.num > 1)
        {
            beep.num --;/* code */           
            beep.now_time = 0;
        }
        else
        {
           beep.loop_state = off;
           beep.now_time = 0;
           beep_off(); 
        } 
        beep.now_time += beep.loop_time;
    }
    else 
    {
        beep_off(); 
        beep.now_time = 0;
    }
}

void beep_init(void)
{
    gpio_init(P33_10, GPO, 0, GPO_PUSH_PULL);
    beep.state = off;
    beep.loop_state = off;
    beep.on_time = 0;
    beep.close_time = 0;
    beep.num = 0;
    beep.loop_time = 1;
    beep.now_time = 0;

}
